package com.voxel.sense.infomsg.module.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.example.com.common.app.BaseActivity;
import com.example.com.common.utils.KeyboardUtils;
import com.example.com.common.utils.ToastUtil;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.voxel.sense.infomsg.R;

import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.constants.EventBusConstants;
import com.voxel.sense.infomsg.module.common.SlidingTabPageAdapter;
import com.voxel.sense.infomsg.module.firm.search.SearchFirmFrag;
import com.voxel.sense.infomsg.module.user.search.SearchUserFrag;
import cyy.example.com.facotry.Factory;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 主页的搜索界面
 * Created by ChenYiYao on 2018/8/7.
 */
public class SearchActivity extends BaseActivity {
    @BindView(R.id.et_search)
    EditText et_search; //搜索输入框
    @BindView(R.id.stl_tip)
    SlidingTabLayout stl_tip;
    @BindView(R.id.vp_content)
    ViewPager vp_content;

    public static void show(Context context) {
        context.startActivity(new Intent(context, SearchActivity.class));
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initData() {
        super.initData();
        et_search.setHint(getString(R.string.title_search_hint_firm));
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        String[] mTitle = {"公司", "人员"};
        List datas = new ArrayList();
        Bundle bundle = new Bundle();
        bundle.putString(IntentConstants.STRING_TYPE, getIntent().getStringExtra(IntentConstants.STRING_TYPE));
        datas.add(Fragment.instantiate(this, SearchFirmFrag.class.getName()));
        datas.add(Fragment.instantiate(this, SearchUserFrag.class.getName(),bundle));
        FragmentPagerAdapter adapter = new SlidingTabPageAdapter(getSupportFragmentManager(), datas, mTitle);
        vp_content.setAdapter(adapter);
        stl_tip.setViewPager(vp_content);
    }

    @Override
    protected void initEven() {
        super.initEven();
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) {
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_NONE ||
                        actionId == EditorInfo.IME_ACTION_SEARCH ||
                        actionId == EditorInfo.IME_ACTION_GO) {
                    //点击搜索，隐藏键盘
                    KeyboardUtils.hideSoftInput(SearchActivity.this);
                    String trim = v.getText().toString().trim();
                    trrigeSearch(trim);
                }
                return false;
            }
        });

        stl_tip.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 0) {
                    et_search.setHint(getString(R.string.title_search_hint_firm));
                } else {
                    et_search.setHint(getString(R.string.title_sear_input_user));
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void trrigeSearch(String trim) {
        int currentTab = stl_tip.getCurrentTab();
        if (TextUtils.isEmpty(trim)) {
            ToastUtil.showToastLong(Factory.app(), "请输入搜索条件");
        } else {
            //先判断是要搜索公司还是用户
            String key = null;
            if (currentTab == 0) {
                key = EventBusConstants.SEARCH_FIRM;
            } else {
                key = EventBusConstants.SEARCH_STAFF;
            }
            //发送通知，进行搜索
            EventBus.getDefault().post(new EventEntity(key, trim));
        }
    }

    @OnClick({R.id.btn_search_left, R.id.bt_search})
    void onClickBack(View view) {
        if (view.getId() == R.id.btn_search_left) {
            this.onBackPressed();
        } else if (view.getId() == R.id.bt_search) {
            String s = et_search.getText().toString();
            trrigeSearch(s);
        }
    }

}
